/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.Impl;

import dao.AccountDao;
import dao.StoreDao;
import entity.Petstore;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ZZ
 */
public class StoreDaoImpl extends BaseDao implements StoreDao{

    @Override
    public int add(Petstore store) {
         int r=0;
        try {
            Connection con=BaseDao.getConnection();
            PreparedStatement ppst=con.prepareStatement("insert into petstore(ID,NAME,PASSWORD,BALANCE) VALUES(?,?,?,?)");
            ppst.setInt(1, store.getId());
            ppst.setString(2,store.getName());
            ppst.setString(3,store.getPassword());
            ppst.setDouble(4,store.getBalance());
            r=ppst.executeUpdate();
            ppst.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(OwnerDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }

    @Override
    public void update(int id,  double balance) {
 try {
            Connection con=BaseDao.getConnection();
            PreparedStatement ppst=con.prepareStatement("update petstore set BALANCE=? WHERE ID="+id);
            ppst.setDouble(1,balance);
            ppst.executeUpdate();
            ppst.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public List<Petstore> select() {
        Petstore store;
        List child = new ArrayList();
        try {
            Statement statement=BaseDao.getConnection().createStatement();
            ResultSet rs=statement.executeQuery("select * from petstore");
            while (rs.next()) {
                store=new Petstore(rs.getInt("ID"), 
                        rs.getString("NAME"),
                        rs.getString("PASSWORD"),
                        rs.getDouble("BALANCE")
                        );
                
                child.add(store);
                
            }
            
           BaseDao.closeConnection();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return child;
    }
    
}
